package com.sum.slike.simple.slice;

import com.sum.slike.SuperLikeLayout;
import com.sum.slike.simple.BitmapProviderFactory;
import com.sum.slike.simple.ResourceTable;
import com.sum.slike.utils.LogUtil;
import com.sum.slike.utils.PixelUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

/**
 * @author: zhu_zhonglin
 * @time: 2021/4/26
 */
public class SimpleAbilitySlice extends AbilitySlice {

    SuperLikeLayout superLikeLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_simple);
    }

    @Override
    public void onActive() {
        super.onActive();
        superLikeLayout = (SuperLikeLayout) findComponentById(ResourceTable.Id_super_like_layout);
        superLikeLayout.setProvider(BitmapProviderFactory.getHDProvider(this));
        findComponentById(ResourceTable.Id_like_btn).setClickedListener(new Component.ClickedListener() {
            long duration = 2000;
            long lastClickTime;

            @Override
            public void onClick(Component component) {
                if (System.currentTimeMillis() - lastClickTime > duration) { // 防抖
                    component.setSelected(!component.isSelected());
                }
                lastClickTime = System.currentTimeMillis();
                if (component.isSelected()) {
                    int x = (int) component.getTranslationX() + PixelUtil.screenWidth() / 2;
                    int y = (int)component.getTranslationY() + PixelUtil.screenHeight() / 2;
                    LogUtil.loge("x:" + x + " TranslationX:" + component.getTranslationX() + " y: " + y + " TranslationY: " + component.getTranslationY());
                    superLikeLayout.launch(x, y);
                }

            }
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
